﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.IO;

using System.Drawing.Imaging;

using submgr = Microsoft.Dss.Services.SubscriptionManager;
using webcam = Microsoft.Robotics.Services.WebCam.Proxy;


namespace AI
{
    

    public partial class AIInfo : Form
    {
        private AIOperations _mainPort;
        //webcam.WebCamOperations _webcamPort;

        //Bitmap _MapImage;


        public MyCar _MyCar = new MyCar();

        public DestInfo _DestInfo = new DestInfo();

        //Image _MapImage;

        EditMode.iEditMode _CurMode;
        Dictionary<String , EditMode.iEditMode> _ModeList = new Dictionary<string,AI.EditMode.iEditMode>();

        PictureBox _PictureBuffer = new PictureBox();

        public AIInfo(AIOperations mainPort)
        {
//            _webcamPort = webcamPort;

            _mainPort = mainPort;

            InitializeComponent();

            this.propertyPathInfo.SelectedObject = _DestInfo;
            this.propertyCarInfo.SelectedObject = _MyCar;

            timer1.Start();
            //timer2.Start();


            

            //pictureMap.BackColor = Color.White;
        }


        //private void BMP2Point()
        //{
        //    Bitmap bitmap = new Bitmap("robochamps/urban/AI/point.bmp");

        //    BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

        //    unsafe
        //    {
        //        byte* imgPtr = (byte*)(data.Scan0);
        //        for (int i = 0; i < data.Height; i++)
        //        {
        //            for (int j = 0; j < data.Width; j++)
        //            {
        //                // write the logic implementation here
        //                imgPtr += 3;
        //            }
        //            imgPtr += data.Stride - data.Width * 3;
        //        }
        //    }

        //    bitmap.UnlockBits(data);
        //}

        private void AIInfo_Load(object sender, EventArgs e)
        {
            //String pat = Directory.GetCurrentDirectory();

            //_MapImage = Image.FromFile("robochamps/urban/AI/map.jpg");

            
            //pictureMap.Image = _MapImage;


            Vector po = MathUtil.GetNextPos(1,0, 5, 0);

            AddLog(po.ToString());

            InitMap(pictureMap.CreateGraphics());

            _MIM.Load();

            _CurMode = new EditMode.AddPath(this);
            _ModeList.Add(_CurMode.GetType().Name, _CurMode);


            _CurMode = new EditMode.AddKIAPoint(this);
            _ModeList.Add(_CurMode.GetType().Name, _CurMode);


            _CurMode = new EditMode.AddNode(this);
            _ModeList.Add(_CurMode.GetType().Name, _CurMode);



            _CurMode = new EditMode.AddPoint(this);
            _ModeList.Add(_CurMode.GetType().Name, _CurMode);

            _CurMode = new EditMode.PathTest(this);
            _ModeList.Add(_CurMode.GetType().Name, _CurMode);


        }


        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void label8_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label9_Click(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            
            //_g.FillEllipse(

            pictureMap.Refresh();
            //pictureMap.Image = _MapImage;
            //_g.DrawImage(_MapImage,0,0,1066,727);
            //System.Threading.Thread.Sleep(1000);

            Draw();

            //pictureMap.Image = _PictureBuffer.Image;

            //_g.DrawEllipse(_Cur_Pen, 10, 10, 20, 20);

        }

        private void pictureMap_Click(object sender, EventArgs e)
        {

        }

        private void pictureMap_MouseUp(object sender, MouseEventArgs e)
        {
            _CursorPoint.X = ((1200.0 - (double)e.X) / 2.0 + 12232000.0) / 100000.0;
            _CursorPoint.Y = ((700.0 - (double)e.Y)/ 2.0 + 4760000.0) / 100000.0;

            
            //MouseEventArgs temp = new MouseEventArgs(e.Button, e.Clicks, e.X, 727 - e.Y, e.Delta);

            _CurMode.MouseClick(e.Button, _CursorPoint.X, _CursorPoint.Y);
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            _CurMode = _ModeList[tabControl.SelectedTab.Text];

            //tabControl.SelectedTab.t
        }

        private void MapInfo_Click(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            propertyGrid1.SelectedObject = listBox1.SelectedItem;
            
        }

        private void AddLine_Click(object sender, EventArgs e)
        {

        }

        private void Save_Click(object sender, EventArgs e)
        {
            _MIM.Save();
            
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void PathTest_Click(object sender, EventArgs e)
        {

        }

        private void Remove_Click(object sender, EventArgs e)
        {

        }


        private void listBox4_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
